<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $where   = [];
        $request = input('request.');

        if (!empty($request['uid'])) {
            $where['id'] = intval($request['uid']);
        }
        $keywordComplex = [];
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $keywordComplex['user_login|user_nickname|user_email|mobile']    = ['like', "%$keyword%"];
        }
        $usersQuery = Db::name('user');

        $list = $usersQuery->whereOr($keywordComplex)->where($where)->order("create_time DESC")->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 1])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功！", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 1])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }
  
  
    /**
     * 体验卡计次
     * @adminMenu(
     *     'name'   => '体验卡计次',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '体验卡计次',
     *     'param'  => ''
     * )
     */
    function add_card_num(){
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $user_info=Db::name("user")->where(["id" => $id])->find();
            if($user_info['card_num']==7){
                $this->error('该会员已经没有次数了！');
            }
            $result = Db::name("user")->where(["id" => $id])->setInc('card_num');
            if ($result) {
                $this->success("操作成功！", "adminIndex/index");
            } else {
                $this->error('操作失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 体验卡消次
     * @adminMenu(
     *     'name'   => '体验卡消次',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '体验卡消次',
     *     'param'  => ''
     * )
     */
    function del_card_num(){
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $user_info=Db::name("user")->where(["id" => $id])->find();
            if($user_info['card_num']==0){
                $this->error('该会员还未开始用卡，无法减少！');
            }
            $result = Db::name("user")->where(["id" => $id])->setDec('card_num');
            if ($result) {
                $this->success("操作成功！", "adminIndex/index");
            } else {
                $this->error('操作失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
  
  /**
     * 免费卡计次
     * @adminMenu(
     *     'name'   => '免费卡计次',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '免费卡计次',
     *     'param'  => ''
     * )
     */
    function add_m_card_num(){
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $user_info=Db::name("user")->where(["id" => $id])->find();
            if($user_info['m_card_num']==2){
                $this->error('该会员已经没有次数了！');
            }
            $result = Db::name("user")->where(["id" => $id])->setInc('m_card_num');
            if ($result) {
                $this->success("操作成功！", "adminIndex/index");
            } else {
                $this->error('操作失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 免费卡消次
     * @adminMenu(
     *     'name'   => '免费卡消次',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '免费卡消次',
     *     'param'  => ''
     * )
     */
    function del_m_card_num(){
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $user_info=Db::name("user")->where(["id" => $id])->find();
            if($user_info['m_card_num']==0){
                $this->error('该会员还未开始用卡，无法减少！');
            }
            $result = Db::name("user")->where(["id" => $id])->setDec('m_card_num');
            if ($result) {
                $this->success("操作成功！", "adminIndex/index");
            } else {
                $this->error('操作失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
   
 
   /**
     * 推荐用户
     * @adminMenu(
     *     'name'   => '推荐用户',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '推荐用户',
     *     'param'  => ''
     * )
     */
    public function index_tj()
    {
        $id=cmf_get_current_admin_id();
        $keywordComplex = [];
        if($id!=1){
            $keywordComplex['tj_user']=$id;
        }
        $where   = [];
        $request = input('request.');

        if (!empty($request['uid'])) {
            $where['id'] = intval($request['uid']);
        }

        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $keywordComplex['user_login|user_nickname|user_email|mobile']    = ['like', "%$keyword%"];
        }
        $usersQuery = Db::name('user');

        $list = $usersQuery->whereOr($keywordComplex)->where($where)->order("create_time DESC")->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }
}
